package ch.myDungeons.view;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import ch.myDungeons.game.MapDisplay;
import ch.myDungeons.game.MyDungeon;


/**
 * The JPanel that paints the current map
 * @author Grii
 *
 */
public class MapView extends JPanel {
	private static final long serialVersionUID = 4747256786836867918L;
	
	
	/** The text area containing the map */
	private StyledDocument doc = new DefaultStyledDocument();
	private JTextPane mapField = new JTextPane (doc);
	
	InfoView infoView;
	
	MapDisplay mapDisplay;
	
	/** Constructor of the map */
	public MapView(int sizeX, int sizeY, InfoView infoView){
		this.setLayout(null);
		this.setSize(sizeX, sizeY);
		this.setBackground(Color.white);
		this.infoView = infoView;
		
		mapField.addCaretListener(selectionListener);
		mapField.setSize(sizeX, sizeY);
		mapField.setLocation(0, 0);
		mapField.setEditable(false);
		mapField.setFont(new Font(Font.MONOSPACED, 0, 12));

		add(mapField);
	}
	
	public void setMapDisplay(MapDisplay mapDisplay){
		this.mapDisplay = mapDisplay;
	}

	/** Prints out the new map */
	public void updateMap(String map, ArrayList<Integer> health){
		mapField.setText("Turn " + MyDungeon.game.turnNumber + "\nDepth: " + MyDungeon.game.mapLevel + "\n" + map);
		SimpleAttributeSet set = new SimpleAttributeSet();
		StyleConstants.setForeground(set, Color.BLACK);
		doc.setCharacterAttributes(0, mapField.getDocument().getLength(), set, false);
		
		int personIndex = 0;
		int introLength = 10 + (int)(Math.log10(MyDungeon.game.turnNumber)) + 5;
		if (MyDungeon.game.mapLevel == 0) {
			introLength += 0;
		} else {
			introLength += (int)(Math.log10(MyDungeon.game.mapLevel));
		}
		for (int i = introLength; i < mapField.getDocument().getLength(); i++) {
        	try {
				if(mapField.getText(i, 1).equals("P") || mapField.getText(i, 1).equals("E") || mapField.getText(i, 1).equals("A")){
					set = new SimpleAttributeSet();
					StyleConstants.setForeground(set, new Color(255-health.get(personIndex), 0, 0));
					personIndex++;
					doc.setCharacterAttributes(i, 1, set, false);
				}
			} catch (BadLocationException e){}
        }
	}
	
	/** Displays the information about the selected tile */
	public void showInfo(){
		int pos = mapField.getSelectionStart();
		pos -= 10 + (int)(Math.log10(MyDungeon.game.turnNumber)) + 6;
		if (MyDungeon.game.mapLevel == 0) {
			pos -= 0;
		} else {
			pos -= (int)(Math.log10(MyDungeon.game.mapLevel));
		}
		pos -= mapDisplay.map.tiles.length + 2 + 1;
		if (pos < 0) {
			infoView.updateInfo(MyDungeon.game.player.getInfo());
		} else {
			int posY = pos / (mapDisplay.map.tiles.length + 1 + 2);
			int posX = pos - posY * (mapDisplay.map.tiles.length + 1 + 2) - 1;
			if (posX < 0 || posY < 0 || posY >= mapDisplay.map.tiles[0].length || posX >= mapDisplay.map.tiles.length) {
				infoView.updateInfo(MyDungeon.game.player.getInfo());
			} else {
				infoView.updateInfo(mapDisplay.getInfo(posX, posY));
			}
		}
		//Console.consoleInput.requestFocus();
	}
	
	/** Clear everything on the map */
	public void clearMap(){
		mapField.setText("");
	}
	
	private CaretListener selectionListener = new CaretListener() {
		@Override
		public void caretUpdate(CaretEvent e) {
			showInfo();
		}
	};
}
